package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3GlideRoundTransform;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.X3Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenSi on 2018/3/19.
 */

public class X3CameraVisitAdapter extends BaseAdapter {

    private List<String> imageList;
    private Context mContext;
    private View mView;
    private final RequestOptions myOptions;


    public X3CameraVisitAdapter(Context mContext) {
        this.imageList = new ArrayList<>();
        this.mContext = mContext;
        myOptions = new RequestOptions()
                .centerCrop()
                .transform(new X3GlideRoundTransform(mContext, 7));
    }

    public void changeCamera(List<String> imageList) {
        this.imageList = imageList;
        notifyDataSetChanged();
    }

    public List<String> getImageList() {
        return imageList;
    }

    @Override
    public int getCount() {
        return imageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return imageList.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        if (view == null) {
            view = View.inflate(mContext, R.layout.x3_camera_include_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_image = view.findViewById(R.id.iv_image);
            viewHolder.image_delete = view.findViewById(R.id.image_delete);
            viewHolder.iv_play = view.findViewById(R.id.iv_play);
            view.setTag(viewHolder);
            this.mView = view;
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Log.e("imageSize", imageList.size() + "  ");

        if (i == imageList.size()) {
            // 拍照模块
            boolean zhRCN = LanguageUtiles.isZhRCN();
            if (zhRCN) {
                Glide.with(mContext).load(R.mipmap.x3_camera_wx).apply(new RequestOptions()
                        .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)).into(viewHolder.iv_image);
            }else {
                Glide.with(mContext).load(R.mipmap.x3_camera_wx_en).apply(new RequestOptions()
                        .skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)).into(viewHolder.iv_image);
            }


            viewHolder.image_delete.setVisibility(View.GONE);
            viewHolder.iv_play.setVisibility(View.GONE);

            // 可以拍照
            viewHolder.iv_image.setVisibility(View.VISIBLE);

            viewHolder.iv_image.setOnClickListener(v -> {

                if (iOnItemCameraClick != null) {
                    iOnItemCameraClick.iOnItemCameraClick();
                }
            });

        } else {
            // 照片模块

            viewHolder.iv_play.setVisibility(View.GONE);

            Glide.with(mContext).load(imageList.get(i)).apply(new RequestOptions().
                    apply(myOptions).error(R.mipmap.x3_car_img_bg).
                    placeholder(R.mipmap.x3_car_img_bg)).thumbnail(0.1f).into(viewHolder.iv_image);

            viewHolder.image_delete.setVisibility(View.VISIBLE);
            viewHolder.iv_image.setClickable(false);
            viewHolder.iv_image.setOnClickListener(v -> {
                X3Utils.startShowImage(mContext,imageList,i);
            });

        }

        viewHolder.image_delete.setOnClickListener(view1 -> {
            if (imageList.size() > 0) {
                imageList.remove(i);
                notifyDataSetChanged();
            }

        });

        return view;
    }

    IOnItemCameraClick iOnItemCameraClick;

    public void setiOnItemCameraClick(IOnItemCameraClick iOnItemCameraClick) {
        this.iOnItemCameraClick = iOnItemCameraClick;
    }

    public interface IOnItemCameraClick {

        void iOnItemCameraClick();

    }

    class ViewHolder {
        ImageView iv_image;
        ImageView image_delete;
        ImageView iv_play;
    }

}
